//
//  HomeSubController.m
//  ScrollViewDemo
//
//  Created by SmartStrategy on 2019/4/8.
//  Copyright © 2019 SmartStrategy. All rights reserved.
//

#import "HomeSubController.h"
#import <MJRefresh.h>

@interface HomeSubController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray<NSString *> *dataSource;

@end

@implementation HomeSubController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];
    [self setupUI];
    [self initWithData];
}

- (void)setupUI {
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf refreshData];
    }];
    
    MJRefreshBackNormalFooter *refreshFooter = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf getMoreData];
    }];
    refreshFooter.stateLabel.hidden = YES;
    self.tableView.mj_footer = refreshFooter;
    
    [self.view addSubview:self.tableView];
    
    self.isRefresh = YES;
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)initWithData {
    [self.dataSource removeAllObjects];
    [self.dataSource addObjectsFromArray:[self fetchAddDataSource]];
    [self.tableView reloadData];
}

//- (void)scrollToTop {
//    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
//}

- (void)refreshData {
    [self initWithData];
    [self.tableView.mj_header endRefreshing];
}

- (void)getMoreData {
    [self.dataSource addObjectsFromArray:[self fetchAddDataSource]];
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshing];
}

// 每次加载的数据源
- (NSMutableArray<NSString *> *)fetchAddDataSource {
    NSMutableArray<NSString *> *array = [NSMutableArray arrayWithCapacity:10];
    for (int i = 0; i < 10; i ++) {
        [array addObject:@"测试"];
    }
    return array;
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *rid = @"HomeSubACell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rid];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rid];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"测试 -- %ld", indexPath.row];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (!self.vcCanScroll) {// 当前不能滚动
        
        if (self.isRefresh) {
            return;
        }
        scrollView.contentOffset = CGPointZero;
    }
    
    if (scrollView.contentOffset.y <= 0) {
        
        self.vcCanScroll = NO;
        scrollView.contentOffset = CGPointZero;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeScrollStatus" object:nil userInfo:nil];
    }
}


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = YES;
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (NSMutableArray<NSString *> *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
